package com.hankol.message

import com.baselibrary.Constants
import com.baselibrary.bean.DynamicInfoData
import com.baselibrary.extension.checkNotNull
import com.baselibrary.manager.SensorsDataManager
import com.baselibrary.utils.HKLogUtils
import com.hankol.message.bean.DynamicCardMessage
import com.hankol.message.bean.FriendStateMessage
import io.rong.imkit.RongIM
import io.rong.imlib.IRongCallback
import io.rong.imlib.RongIMClient
import io.rong.imlib.model.Conversation
import io.rong.imlib.model.Message
import io.rong.message.TextMessage


//发送自定义消息
object HKIMUtils {
    //动态
    fun sendMessage4DynamicCard(targetIds: List<String>, bean: DynamicInfoData) {
        SensorsDataManager.instance.privateLetter(OptionUtils.fromShowTypeStr(bean.type), if (targetIds.size == 1) "单发" else "群发")
        for (targetId in targetIds) {
            bean.apply {
                val dynamicBeanMsg = DynamicCardMessage.obtain(id, type, cover, if (title.startsWith(" # ")) title.substring(" # ".length) else title, heardUrl, nickName, isFans, path, liveState, sendMessage)
                val dynamicMsg = Message.obtain(targetId, Conversation.ConversationType.PRIVATE, dynamicBeanMsg)
                //打开或关闭某条消息的可扩展属性。
                dynamicMsg.setCanIncludeExpansion(true)
                if (type == Constants.DYNAMIC_TYPE_LIVE) {
                    val expansionKey = HashMap<String, String>()
                    expansionKey.put("liveState", "${liveState}")
                    dynamicMsg.setExpansion(expansionKey);
                }

                sendMsg(dynamicMsg, type)
                if (sendMessage.checkNotNull()) { //附加 文本消息
                    val messageContent = TextMessage.obtain(sendMessage)
                    val message = Message.obtain(targetId, Conversation.ConversationType.PRIVATE, messageContent)
                    sendMsg(message, type)
                }
            }
        }
    }

    fun showTypeStr(type: Int): String {
        //    分享【图文动态】、分享【视频动态】、分享【专栏文章】、分享【话题】、分享【直播间】
        var str = ""
        when (type) {
            Constants.DYNAMIC_TYPE_IMG -> {
                str = "分享[图文动态]"
            }
            Constants.DYNAMIC_TYPE_VIDEO -> {
                str = "分享[视频动态]"
            }
            Constants.DYNAMIC_TYPE_TOPIC -> {
                str = "分享[话题]"
            }
            Constants.DYNAMIC_TYPE_LIVE -> {
                str = "分享[直播间]"
            }
        }
        return str
    }

    fun sendMsg(msg: Message, type: Int) {
        RongIM.getInstance().sendMessage(msg, showTypeStr(type), showTypeStr(type), object : IRongCallback.ISendMessageCallback {
            override fun onAttached(message: Message) {
                HKLogUtils.e("======发送动态onAttached===========$message")
            }

            override fun onSuccess(message: Message) {
                HKLogUtils.e("======发送动态onSuccess===========$message")
            }

            override fun onError(message: Message, errorCode: RongIMClient.ErrorCode) {
                HKLogUtils.e("======发送动态onError===========$message==========$errorCode")
            }
        })
    }

    //type:   0:TA不是好友，不能进行聊天 1:由于对方没有关注你，限制发送3条消息 2:你没有关注TA，不能进行聊天
    fun sendMessage4FriendState(targetId: String, type: Int = 0) {
        val friendStateMsg = FriendStateMessage.obtain(type)
        RongIM.getInstance().insertOutgoingMessage(Conversation.ConversationType.PRIVATE, targetId, Message.SentStatus.SENT, friendStateMsg, object : RongIMClient.ResultCallback<Message>() {
            override fun onSuccess(message: Message) {

            }

            override fun onError(p0: RongIMClient.ErrorCode?) {
                HKLogUtils.e("----------insertOutgoingMessage--onError-------------" + p0)
            }
        })
    }

    //关注成功后发送消息
    @JvmStatic
    fun followSendMsg(uid: Int, friendState: Int) {
        HKLogUtils.e("================uid:${uid}=====friendState:${friendState}=====")
        if (friendState == 4&&uid!=0) {
            val messageContent = TextMessage.obtain("我们已经成为好友，开始聊天吧")
            messageContent.extra = "${friendState}"
            val message = Message.obtain("${uid}", Conversation.ConversationType.PRIVATE, messageContent)
            sendMsg(message, 0)
        }
    }
}
